// pages/my/accountBinding/bindingPhone/bindingPhone.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    confirmphone: "",
    code: "000000",
    SMSCode: "",
    forbidden:false,
    buttonText:'获取验证码',
    Type:'primary'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  setphone: function (e) {
    this.setData({
      phone: e.detail.value,
    });
  },
  setcode: function (e) {
    this.setData({
      code: e.detail.value,
    });
  },
  checkphone: function () {
    var phoneNum = /^1\d{10}$/;
    if (!phoneNum.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式有误',
        icon: 'success',
        duration: 1500
      })
      return;
    }
    else{
      this.time();
    }

  },

  //倒计时
  time:function(){
    var t=60;
  
    var that=this;
    function TimeCount()
    {
      t--;
      if(t<=0)
      {
        that.setData({
          forbidden:false,
          buttonText: '获取验证码',
          Type:'primary'
        })
      }
      else{
        that.setData({
          forbidden: true,
          buttonText:'重新发送（'+t+'）',
          Type: 'warn'
        })
        setTimeout(TimeCount, 1000);
      }
  
    }
   setTimeout(TimeCount,100);


  },
  sendSMS: function (e) {
    var that = this;
    if (this.data.phone == LApp.globalData.userInfo.Modile) {
      wx.showModal({
        title: '提示',
        content: '该手机已绑定当前账号',
      });
      return;
    }
    this.checkphone();

    LApp._get({
      url: "nbapi/my/sendsms",
      data: { mobile: this.data.phone, name: LApp.globalData.userInfo.RealName },
      success: function (r) {
        that.setData({
          SMSCode: r.code,
          confirmphone: that.data.phone,
        });
      },
      fail: function (r) {
        wx.showToast({
          title: r,
          icon: 'success',
          duration: 1000
        });
      }
    });
  },
  confirm: function () {   
    if (this.data.code == "") {
      wx.showToast({
        title: "请输入验证码！",
        icon: 'success',
        duration: 1000
      });
      return;
    }
    if (this.data.code != this.data.SMSCode) {
      wx.showToast({
        title: "验证码错误！",
        icon: 'success',
        duration: 1000
      });
      return;
    }
    var that = this;
    LApp._get({
      url: "nbapi/my/updatephone",
      data: { userid: LApp.globalData.userInfo.UserID, mobile: this.data.confirmphone },
      success: function (r) {
        LApp.globalData.userInfo.Modile = that.data.confirmphone;
        that.setData({
          code: "000000",
          SMSCode: "",
        });
        wx.showToast({
          title: r,
          icon: 'success',
          duration: 2000
        });
        wx.reLaunch({
          url: '../../../my/my',
        })
      },
      fail: function (r) {
        wx.showToast({
          title: r,
          icon: 'success',
          duration: 1000
        });
      }
    });
  }
})